<?php
/**
 * Rsa 加密算法
 *
 * @author Lv Qiaoyi <38338051@qq.com>
 * @since 2020-01-22
 */
class Rsa
{
    private static $_config = [
        'public_key' => '',
        'private_key' => '',
    ];

    public static function setPrivateKey($privateKey)
    {
        self::$_config['private_key'] = $privateKey;
    }

    public function setPublicKey($publicKey)
    {
        self::$_config['public_key'] = $publicKey;
    }

    /**
     * @uses 获取私钥
     * @return bool|resource
     */
    private static function _getPrivateKey()
    {
        $priv_key = self::$_config['private_key'] or die('Private key required!');
        $privPem = chunk_split($priv_key, 64, "\n");
        $privPem = "-----BEGIN PRIVATE KEY-----\n".$privPem."-----END PRIVATE KEY-----\n";
        return openssl_get_privatekey($privPem);
    }

    /**
     * @uses 获取公钥
     * @return bool|resource
     */
    private static function _getPublicKey()
    {
        $public_key = self::$_config['public_key'] or die('Public key required!');
        $pubPem = chunk_split($public_key, 64, "\n");
        $public_key = self::url_safe_decode($public_key);
        //$pubPem = "-----BEGIN PUBLIC KEY-----\n".$pubPem."-----END PUBLIC KEY-----\n";
        $pubPem = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($public_key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
        //$pubPem = "-----BEGIN PUBLIC KEY-----\n" . $public_key . "\n-----END PUBLIC KEY-----";
        //echo $pubPem;
        return openssl_get_publickey($pubPem);
        //return openssl_pkey_get_public($pubPem);
    }
    
    public static function url_safe_decode($string)
    {
        $data = str_replace(['-','_'], ['+','/'], $string);
        $mod4 = strlen($data) % 4;
        if ($mod4)
        {
            $data .= substr('====', $mod4);
        }
        return $data;
    }

    /**
     * @uses 私钥加密
     * @param string $data
     * @return null|string
     */
    public static function privEncrypt($data = '')
    {
        if (!is_string($data)) {
            return null;
        }
        return openssl_private_encrypt($data, $encrypted, self::_getPrivateKey()) ? base64_encode($encrypted) : null;
    }

    /**
     * @uses 公钥加密
     * @param string $data
     * @return null|string
     */
    public function publicEncrypt($data = '')
    {
        if (!is_string($data)) {
            return null;
        }
        //echo openssl_public_encrypt($data, $encrypted, self::_getPublicKey()) ? base64_encode($encrypted) : null;
        return openssl_public_encrypt($data, $encrypted, self::_getPublicKey()) ? base64_encode($encrypted) : null;
    }

    /**
     * @uses 私钥解密
     * @param string $encrypted
     * @return null
     */
    public static function privDecrypt($encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::_getPrivateKey())) ? $decrypted : null;
    }

    /**
     * @uses 公钥解密
     * @param string $encrypted
     * @return null
     */
    public static function publicDecrypt($encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::_getPublicKey())) ? $decrypted : null;
    }

}